package org.sadko.boozemeter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class ResultShower extends Activity{
	public static final long RESULT_BORDER_0_1 = 15000;
	public static final long RESULT_BORDER_1_2 = 30000;
	public static final long RESULT_BORDER_2_3 = TremorSensor.SCORE_NORMAL;
	public static final long RESULT_BORDER_3_4 = 80000;
	public static final long RESULT_BORDER_4_5 = 150000;
	public static final long RESULT_BORDER_5_6 = 225000;
	public static final long RESULT_BORDER_6_7 = TremorSensor.SCORE_DRINKING;
	public static final long RESULT_BORDER_7_8 = 500000;
	public static final long RESULT_BORDER_8_9 = 800000;
	public static final long RESULT_BORDER_9_10 = TremorSensor.SCORE_DRUNK;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.results);
		
		Intent intent = getIntent();
		long result = intent.getLongExtra("Result", 0);
				
		TextView tv = (TextView) findViewById(R.id.result);
		tv.setText(String.valueOf(result));
		
		if(result < RESULT_BORDER_0_1)
			tv.setText("0 / 10");
		else if(result < RESULT_BORDER_1_2)
			tv.setText("1 / 10");
		else if(result < RESULT_BORDER_2_3)
			tv.setText("2 / 10");
		else if(result < RESULT_BORDER_3_4)
			tv.setText("3 / 10");
		else if(result < RESULT_BORDER_4_5)
			tv.setText("4 / 10");
		else if(result < RESULT_BORDER_5_6)
			tv.setText("5 / 10");
		else if(result < RESULT_BORDER_6_7)
			tv.setText("6 / 10");
		else if(result < RESULT_BORDER_7_8)
			tv.setText("7 / 10");
		else if(result < RESULT_BORDER_8_9)
			tv.setText("8 / 10");
		else if(result < RESULT_BORDER_9_10)
			tv.setText("9 / 10");
		else tv.setText("10 / 10!");
			
		Vibrator vbr = (Vibrator) getSystemService(VIBRATOR_SERVICE);
		vbr.vibrate(500);
		
		ImageView iv = (ImageView) findViewById(R.id.im_result);
		ImageView iv2 = (ImageView) findViewById(R.id.im_result_gr);
		tv = (TextView) findViewById(R.id.result_descr);
		if(result < TremorSensor.SCORE_NORMAL){
			iv.setImageResource(R.drawable.droid_normal);
			iv2.setImageResource(R.drawable.gr_normal);
			tv.setText(R.string.result_string_normal);
		}
		else if(result < TremorSensor.SCORE_DRINKING){
			iv.setImageResource(R.drawable.droid_drinking);
			iv2.setImageResource(R.drawable.gr_drinking);
			tv.setText(R.string.result_string_drinking);
		}
		else if(result < TremorSensor.SCORE_DRUNK){
			iv.setImageResource(R.drawable.droid_drunk);
			iv2.setImageResource(R.drawable.gr_drunk);
			tv.setText(R.string.result_string_drunk);
		}
		else {
			iv.setImageResource(R.drawable.droid_yeltsin);
			iv2.setImageResource(R.drawable.gr_yeltsin);
			tv.setText(R.string.result_string_boris);
		}
		
		
		Button b = (Button) findViewById(R.id.button_back);
		b.setOnClickListener(new OnClickListener(){
			public void onClick(View v){
				finish();
			}
		});
		
		b = (Button) findViewById(R.id.button_exit);
		b.setOnClickListener(new OnClickListener(){
			public void onClick(View v){
				Messenger mMesngr = new Messenger(BoozeMeter.mHndl);
				Message msg = Message.obtain();
				msg.what = BoozeMeter.MESSAGE_EXIT;
				try {
					mMesngr.send(msg);
				} catch (RemoteException e) {
					e.printStackTrace();
				}
				finish();
			}
		});
	}

}
